package com.jy.adscm.operation.cg.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.cg.bean.CgBomSupplier;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.bean.QueryCgMaterial;
import com.jy.adscm.operation.cg.mapper.mysql.CgBomSupplierMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.cg.service.CgMaterialService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.purchase.bean.Supplier;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 基础物料表 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-01
 */
@Service
public class CgMaterialServiceImpl extends ServiceImpl<CgMaterialMapper, CgMaterial> implements CgMaterialService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    CgBomSupplierMapper cgBomSupplierMapper;
    @Override
    public Page<CgMaterial> selectCgMaterial(Page<CgMaterial> page, QueryCgMaterial queryCgMaterial) {

        if (!AdscmBaseUtils.getInstance().isNotEmpty(queryCgMaterial.getSupplier(),null)){
            Wrapper<CgMaterial>  wrapper =new EntityWrapper<>();
            if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgMaterial.getName(),null)){
                wrapper.like("name",queryCgMaterial.getName());
            }

            if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgMaterial.getType(),null)){
                wrapper.eq("mType",queryCgMaterial.getType());
            }
            page= super.selectPage(page,wrapper);
            List<CgMaterial> cgMaterials = page.getRecords();
            for (CgMaterial cgMaterial : cgMaterials) {
                if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getCreator(),null)){
                    JyUser jyUser = jyUserMapper.selectById(cgMaterial.getCreator());
                    cgMaterial.setCreatorName(jyUser.getName());
                }
            }
            page.setTotal(super.selectCount(wrapper));
        }else {
            List<CgMaterial> cgMaterials=new ArrayList<>();
            Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
            supplierWrapper.eq("sid",queryCgMaterial.getSupplier());
            List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
            for (CgBomSupplier cgBomSupplier : cgBomSuppliers) {
                CgMaterial cgMaterial = super.selectById(cgBomSupplier.getBid());
                cgMaterials.add(cgMaterial);
            }
            page.setTotal(cgMaterials.size());
            page.setRecords(cgMaterials);
        }
        return page;
    }

    @Override
    public Page<CgMaterial> selectCgMaterialAndSupplier(Page<CgMaterial> page, QueryCgMaterial queryCgMaterial) {
        Wrapper<CgMaterial>  wrapper =new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgMaterial.getName(),null)){
            wrapper.like("name",queryCgMaterial.getName());
        }
        page= super.selectPage(page,wrapper);
        List<CgMaterial> cgMaterials = page.getRecords();
        for (CgMaterial cgMaterial : cgMaterials) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(cgMaterial.getCreator());
                cgMaterial.setCreatorName(jyUser.getName());
            }
             Wrapper<CgBomSupplier> cgBomSupplierWrapper=new EntityWrapper<>();
            cgBomSupplierWrapper.eq("bid",cgMaterial.getId());
            List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(cgBomSupplierWrapper);
            if (cgBomSuppliers.size()>0){
                cgMaterial.setIsHas(1);
            }else {
                cgMaterial.setIsHas(0);
            }

        }
        page.setTotal(super.selectCount(wrapper));
        return page;
    }

    @Override
    public List<CgMaterial> selectByKeyWords(String name) {
        Wrapper<CgMaterial> cgMaterialWrapper =new EntityWrapper<>();
        cgMaterialWrapper.like("name",name);
        List<CgMaterial> cgMaterials = super.selectList(cgMaterialWrapper);
        return cgMaterials;
    }


}
